#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 355;
const int maxm = 45;
int dp[maxm][maxm][maxm][maxm];
int n,m;
int usec[5];
int card[5],a[maxn];

int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	int tmp;
	for(int i=1;i<=m;i++){
		cin>>tmp;
		card[tmp]++;
	}
	for(int i=0;i<=card[1];i++)
		for(int j=0;j<=card[2];j++)
			for(int k=0;k<=card[3];k++)
				for(int l=0;l<=card[4];l++){
					int now = i+j*2+k*3+l*4+1;
					if(i>=1){
						dp[i][j][k][l]=max(dp[i][j][k][l],dp[i-1][j][k][l]);
					}
					if(j>=1){
						dp[i][j][k][l]=max(dp[i][j][k][l],dp[i][j-1][k][l]);
					}
					if(k>=1){
						dp[i][j][k][l]=max(dp[i][j][k][l],dp[i][j][k-1][l]);
					}
					if(l>=1){
						dp[i][j][k][l]=max(dp[i][j][k][l],dp[i][j][k][l-1]);
					}
					if(i==0&&j==0&&k==0&&l==0){
						dp[i][j][k][l]=a[1];
					}else{
						dp[i][j][k][l]+=a[now];
					}
				}
				
	cout<<dp[card[1]][card[2]][card[3]][card[4]]<<endl;
	 
}
